package com.kaoqin.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaoqin.mapper.SignRuleMapper;
import com.kaoqin.model.SignLog;
import com.kaoqin.model.SignRule;
import com.kaoqin.service.SignRuleService;
import com.kaoqin.util.Constants;
import com.kaoqin.util.DateUtil;
import com.kaoqin.util.SysUtils;
@Service("signRuleService")
@Transactional
public class SignRuleServiceImpl implements SignRuleService {
    @Autowired
    private SignRuleMapper mapper;
    
    @Override
    public List<SignRule> findAll(SignRule signRule) {
        return mapper.findAll(signRule);
    }
    /**
     * 根据签到规则设置签到时间
     * @param signLog 签到对象
     * @param apType 签到类型
     * @throws RuntimeException
     */
    public void updateSignDateByRule(SignLog signLog,String apType) throws RuntimeException{
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	SignRule signRule = new SignRule();
    	signRule.setAmOrPm(apType);
    	Timestamp currentTimestamp = DateUtil.getCurrentTimestamp();
    	String strDate = sdf.format(currentTimestamp).substring(11, 19);
    	signRule.setCurrTime(strDate);
    	List<SignRule> srList = findAll(signRule);
    	if(SysUtils.isNotList(srList)){
    		SignRule sr = srList.get(0);
    		String dateFormat = DateUtil.getDateFormat(new Date());
			 dateFormat=dateFormat+" "+sr.getSignRuleResult();
			 if(Constants.TO_WORK.equalsIgnoreCase(apType)){
				 signLog.setSignToWork(DateUtil.string2Timestap(dateFormat, DateUtil.DATETIME_DEFAULT_FORMAT));
			 }
			 if(Constants.OFF_WORK.equalsIgnoreCase(apType)){
				 signLog.setSignOffWork(DateUtil.string2Timestap(dateFormat, DateUtil.DATETIME_DEFAULT_FORMAT));
			 }
    	}
    }
}
